# http://beautifulpixel.com/articles/2007/04/16/association-proxy-magic

# in ActiveRecord

class Item < ActiveRecord::Base
  def some_method
    ...
  end
end

class Collection < ActiveRecord::Base
  has_many :items
end

# magic
c = Collection.first
c.items.some_method #=> returns an array

# another example

class State < ActiveRecord::Base
  has_many :zips
  def has_zip?(code)
    zips.find_by_code code
  end
end

class Zip < ActiveRecord::Base
  belongs_to :state

  def self.find_by_code(code)
    find :first, 
      :conditions => [
        'start <= :code AND end >= :code', 
        { :code => code }
      ]
  end
end

@state = State.find_by_abbrev('CA')
@state.has_zip?('95472') # => true
#SQL =>  SELECT * FROM zips WHERE (zips.state_id = 1) AND (start <= '95472' AND end >= '95472') LIMIT 1

######################################

class State < Sequel::Model(:states)
  one_to_many :zips, :class => Zip

  # We don't really need the association proxy stuff
  def has_zip?(code)
    zips['start <= ? AND end >= ?', code, code]
  end
end

